import { hexMD5 } from "../../../thirdparty";

// todo jj you can modify
// 本机部署测试环境，因host一致，用port区分环境
function localDeployEnvironmentSet() {
    const port = window.location.port
    if ('8881' === port) {
        return 'Dev'
    } else if ('8882' === port) {
        return 'Sit'
    } else if ('8883' === port) {
        return 'Preproduct'
    } else if ('8884' === port) {
        return 'Product'
    }

    return 'Product'
}

// todo jj you can modify
// 运维服务器根据host区分环境
function serverDeployEnvironmentSet() {
    const hostname = window.location.hostname
    if ('prd.com.cn' === hostname) {
        return 'Product'
    } else if ('dev.com.cn' === hostname) {
        return 'Dev'
    } else if ('sit.com.cn' === hostname) {
        return 'Sit'
    } else if ('pre.com.cn' === hostname) {
        return 'Preproduct'
    }

    return 'Product'
}

function getEnvironmentName() {
    let environmentName = serverDeployEnvironmentSet()
    if (null == environmentName) {
        environmentName = localDeployEnvironmentSet()
        if (null == environmentName) {
            // default
            environmentName = 'Product' // "Product", "Preproduct", "Sit", "Dev"
        }
    }

    return environmentName
}

let environmentName = getEnvironmentName()

const isProductEnvironment = ('Product' === environmentName)
let logLevel = 10
let isDebug = false
if (!isProductEnvironment) {
    logLevel = 0
    isDebug = true
}
let isStorageCrypt = true
if ('Product' !== environmentName && 'Preproduct' !== environmentName) {
    isStorageCrypt = false
}

function getWhoAmI() {
    return ''
}
const whoAmI = getWhoAmI()

function getWhoAmIType() { // 微信小程序、H5 ...
    return ''
}
const whoAmIType = getWhoAmIType()

function urlParameter(name = '') {
    let urlparam = window.location.search === '' ? window.location.hash : window.location.search;
    if (null === urlparam || 0 === urlparam.length) {
        return {}
    }
    const index = urlparam.indexOf('?')
    if (index < 0) {
        return {}
    }
    urlparam = urlparam.slice(index + 1);
    if (urlparam.length < 1) {
        return {};
    }
    const urlparamArray = urlparam.split("&");
    let param = {};
    if (urlparamArray !== null) {
        urlparamArray.forEach(item => {
            if(item.length > 0) {
                let data = item.split('=');
                if (name === data[0]) {
                    param = Object.assign({}, param, { [decodeURIComponent(data[0])]: decodeURIComponent(data[1]) })
                } else if (name === '' || name === undefined) {
                    param = Object.assign({}, param, { [decodeURIComponent(data[0])]: decodeURIComponent(data[1]) })
                }
            }
        })
    }
    return param;
}

const JJAPPConfigChannelNameAPP = 'app'

function getChannelName() {
    return ''
}
const channelName = getChannelName()

function getChannelType() { // 微信小程序、H5、App ...
    return ''
}
const channelType = getChannelType()

const JJAPPConfig = {
    "EnvironmentName": environmentName,
    isProductEnvironment: function () {
        return isProductEnvironment
    },
    isTestEnvironment: function () {
        return !isProductEnvironment
    },

    isAppParasitifer: function () {
        return JJAPPConfigChannelNameAPP === this.channelName
    },

    // eg, if app invoke h5, h5 channel type is app, but whoami type is still h5
    "channelType": channelType,
    "channelName": channelName,

    "whoAmIType": whoAmIType,
    "whoAmI": whoAmI,

    "LogLevel": logLevel, // 0:debug 1:info(or log) 2:warn 3:error 10: product(发布设置此值) 【只能打印比LogLevel级别高的日志】

    "isStorageCrypt": isStorageCrypt, // 存储数据是否加密，true：key和value都加密

    "isDebug": isDebug, // false: 非debug模式；true：debug模式；
};

export default JJAPPConfig;
